
package com.v1.video.option.db;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.v1.video.domain.RecommendAppInfo;
import com.v1.video.headline.zerodelivery.ZeroVideoProvider;

public class RecommendAppDbUtil {

    public static Uri URI_APP = ZeroVideoProvider.buildUri(RecommendAppInfo.class);

    public static boolean addAppInfo(Context context, RecommendAppInfo Info) {
        try {
            int result = updateAppInfo(context, Info);
            if (result == 0) {
                ContentResolver mResolver = context.getContentResolver();
                Uri uri = mResolver.insert(URI_APP, Info.beanToValues());
                // 通过返回的uri可以获取到当条数据插入的行号，插入失败为-1
                String path = uri.getLastPathSegment();
                if (path.equals("-1")) {
                    throw new Exception("insert error");
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static int deleteAppInfo(Context context, RecommendAppInfo info) {
        ContentResolver mResolver = context.getContentResolver();
        int result = mResolver.delete(URI_APP, "packageName=?", new String[] {
                info.getPackageName()
        });
        return result;
    }

    public static int deleteAppInfo(Context context, String packageName) {
        ContentResolver mResolver = context.getContentResolver();
        int result = mResolver.delete(URI_APP, "packageName=?", new String[] {
                packageName
        });
        return result;
    }

    public static int updateAppInfo(Context context, RecommendAppInfo info) {
        ContentResolver mResolver = context.getContentResolver();
        int result = mResolver.update(URI_APP, info.beanToValues(), "packageName=?",
                new String[] {
            info.getPackageName()
                });
        return result;
    }

    /** 数据库中是否有数据 */
    public static boolean isHasData(Context context) {
        ArrayList<RecommendAppInfo> beanList = getAppInfo(context);
        if (beanList.size() >= 1) {
            return true;
        } else {
            return false;
        }

    }

    public static ArrayList<RecommendAppInfo> getAppInfo(Context context) {
        ContentResolver mResolver = context.getContentResolver();
        Cursor cursor = mResolver.query(URI_APP, null, null, null, null);
        ArrayList<RecommendAppInfo> beanList = new ArrayList<RecommendAppInfo>();
//        if(cursor != null){
//            cursor.moveToLast();
//            if(cursor.isLast()){
//                do{
//                    RecommendAppInfo bean = new RecommendAppInfo();
//                    bean.cursorToBean(cursor);
//                    beanList.add(bean);
//                }
//                while (cursor.moveToPrevious());
//            } 
//            cursor.close();
//        }
        if(cursor != null){
            while (cursor.moveToNext()) {
                RecommendAppInfo bean = new RecommendAppInfo();
                bean.cursorToBean(cursor);
                beanList.add(bean);
            }
            cursor.close();
        }
        return beanList;
    }
}
